package com.jdrx.sw.sewage.service;

import com.google.common.collect.Maps;
import com.jdrx.sw.sewage.beans.dto.DevStorageDTO;
import com.jdrx.sw.sewage.beans.dto.UnLimitDevDTO;
import com.jdrx.sw.sewage.beans.entity.DevStoragePO;
import com.jdrx.sw.sewage.beans.vo.DevStorageInfoVO;
import com.jdrx.sw.sewage.beans.vo.DevStorageVO;
import com.jdrx.sw.sewage.common.enums.StorageDevType;
import com.jdrx.sw.sewage.dao.AlarmDAO;
import com.jdrx.sw.sewage.dao.DevStorageDAO;
import com.jdrx.sw.sewage.dao.DevStorageTypeDAO;
import com.jdrx.sw.sewage.dao.DeviceDAO;
import com.jdrx.sw.sewage.service.base.BaseServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DevStorageServiceImpl extends BaseServiceImpl {

    public static final Logger logger = LoggerFactory.getLogger(DevStorageServiceImpl.class);

    @Autowired
    private DevStorageDAO devStorageDAO;
    @Autowired
    private DevStorageTypeDAO devStorageTypeDAO;
    @Autowired
    private DeviceServiceImpl deviceServiceImpl;
    @Autowired
    private AlarmDAO alarmDAO;
    @Autowired
    private DeviceDAO deviceDAO;

    public Integer countDevNum(Integer devType, Long storageTypeId) {
        return devStorageDAO.countDevNum(devType, storageTypeId);
    }

    public List<DevStorageVO> selectDevStorageList(StorageDevType storageDevType, Long storageTypeId) {
        return devStorageDAO.selectDevStorageList(storageDevType.getKey(), storageTypeId);
    }

    public List<Map<Integer, Integer>> selectDevNum(Long storageTypeId) {
        return devStorageDAO.selectDevNum(storageTypeId);
    }

    public DevStorageInfoVO selectDevInfo(StorageDevType storageDevType, Long devId) {
        if (!StorageDevType.ATTACHMENT.getKey().equals(storageDevType.getKey()))
            return devStorageDAO.selectDevInfo(devId);
        else
            return devStorageDAO.selectDevInfo2(devId);
    }

    public DevStorageInfoVO selectUnlimitedDevInfo(UnLimitDevDTO dto) {
        return devStorageDAO.selectUnlimitedDevInfo(dto);
    }

    public void deleteDev(Long storageTypeId, Long devId) {
        int num = devStorageDAO.countById(devId, storageTypeId);
        devStorageDAO.deleteById(devId);
        if (num == 1) {
            devStorageTypeDAO.deleteById(storageTypeId);
        } else {
            List<DevStorageVO> devStorageVOS = devStorageDAO.selectDevStorageList(null, storageTypeId);
            String attach = devStorageVOS.get(0).getAttach();
            if (StringUtils.isEmpty(attach)) attach = "";
            devStorageTypeDAO.update(attach, storageTypeId, null);
        }
    }

    public void updateDev(DevStorageDTO devStorageDTO) {
        DevStoragePO devStoragePO = new DevStoragePO();
        BeanUtils.copyProperties(devStorageDTO, devStoragePO);
        devStorageDAO.updateDev(devStoragePO);
        if (1 == devStorageDTO.getOrderByOne())
            devStorageTypeDAO.update(devStorageDTO.getAttach(), devStorageDTO.getStorageTypeId(), null);

    }

    public void add(DevStoragePO devStoragePO) {
        devStorageDAO.add(devStoragePO);
    }

    public void scrap(Long devId) {
        DevStoragePO devStoragePO = new DevStoragePO();
        devStoragePO.setId(devId);
        devStoragePO.setDevType(StorageDevType.SCRAP.getKey());
        devStoragePO.setScrapTime(new Date());
        devStorageDAO.updateDev(devStoragePO);

        Long aLong = deviceServiceImpl.selectByStorageDevId(devId);
        alarmDAO.deletByDevId(aLong);
        deviceDAO.deletById(aLong);
    }

    @Transactional(rollbackFor = Exception.class)
    @Async("asyncServiceExecutor")
    public void handlePgVal(List<DevStoragePO> pgDsList, String dataNode) {
        if (CollectionUtils.isNotEmpty(pgDsList)) batchUpdateByGatherDevPoiId(pgDsList, dataNode);
    }

    private void batchUpdateByGatherDevPoiId(List<DevStoragePO> pgDsList, String dataNode) {
        Map<String, Object> params = Maps.newHashMap();
        params.put("list", pgDsList);
        params.put("dataNode", dataNode);
        devStorageDAO.batchUpdateById(params);
    }
}
